//
//  AuthorViewController.m
//  喜马拉雅改良版框架
//
//  Created by liuxin on 15/12/22.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "AuthorViewController.h"

@interface AuthorViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,AuthorDelegate>
@property(nonatomic,strong)UICollectionView *myCollection;
@property(nonatomic,strong)NSMutableArray *allArray;

@end

@implementation AuthorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.allArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    [self getData];
    [self createCollectionView];
}
-(void)getData{
    NSString *str = @"http://mobile.ximalaya.com/m/explore_user_index?device=iphone&page=1";
    [AFNetworkTool getUrl:str body:nil result:HXJSON headerFile:nil success:^(id result) {
        NSArray *array = result[@"list"];
        for (NSDictionary *dic in array) {
            LXAuthorModel *temp = [LXAuthorModel new];
            [temp setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:temp];
        }
        NSDictionary *recommendBozhu = result[@"recommendBozhu"];
        NSMutableArray *tempArr = recommendBozhu[@"list"];
      
        LXAuthorModel *temp = [LXAuthorModel new];
        temp.title = @"新晋主播";
        temp.name = @"new";
        temp.list = tempArr;
        [self.allArray insertObject:temp atIndex:1];
        
        [self.myCollection reloadData];
    } failure:^(NSError *error) {
        NSLog(@"网络不给力");
    }];
}
-(void)createCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH-30)/3, 150);
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 30);
    self.myCollection = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    [self.view addSubview:self.myCollection];
    self.myCollection.collectionViewLayout = flowLayout;
    self.myCollection.backgroundColor = [UIColor whiteColor];
    self.myCollection.delegate = self;
    self.myCollection.dataSource  =self;
    [self.myCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    [self.myCollection registerClass:[LXAuthorCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.myCollection registerClass:[LXAuthorCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [[_allArray[section] list] count];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return _allArray.count;
}
-(UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        LXAuthorCollectionReusableView *resuableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"head" forIndexPath:indexPath];
        resuableView.authorDelegate = self;
        resuableView.backgroundColor = [UIColor whiteColor];
        resuableView.title = [[_allArray objectAtIndex:indexPath.section] title];
        resuableView.name = [[_allArray objectAtIndex:indexPath.section] name];
        return resuableView;
    }
    return nil;
}
-(void)moreAuthor:(NSString *)author title:(NSString *)title{
    LXMoreAuthorTableVC *moreAuthor = [LXMoreAuthorTableVC new];
    moreAuthor.category_name = author;
    moreAuthor.Title = title;
    [self.navigationController pushViewController:moreAuthor animated:YES];
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LXAuthorCollectionViewCell *cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.cellDic = [[_allArray objectAtIndex:indexPath.section] list] [indexPath.row];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    LXAuthorTableVC *author = [[LXAuthorTableVC alloc]init];
    author.Uid =[_allArray[indexPath.section] list][indexPath.row][@"uid"];
    author.Title = [_allArray[indexPath.section]title];
    [self.navigationController pushViewController:author animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
